package monte_carlo_concurrent;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.Random;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.concurrent.ExecutionException;

import monte_carlo_sequentiel.MonteCarloSimulation;
import monte_carlo_values.Values;
import monte_carlo_gui.UserInterface;

public class MonteCarloConcurrent {

	public double MonteCarloStandardOption(String callPutFlag, double s,
			double x, double t, double r, double b, double v, int nSteps,
			int nSimulations) {
		double dt, st;
		double sum, drift, vSqrdt;
		int i, j, z;
		Random random = new Random();
		sum = 0.0;
		z = 0;

		dt = t / nSteps;
		drift = (b - (v * v) / 2) * dt;
		vSqrdt = v * (Math.sqrt(dt));

		if(callPutFlag.compareTo("c") == 0) {
			z = 1;
		}
		else if (callPutFlag.compareTo("p") == 0) {
			z = -1;
		}

		//for (i = 0; i < nSimulations; i++) {
			st = s;
			for (j = 0; j < nSteps; j++) {
				st = st * Math.exp(drift + vSqrdt * random.nextGaussian());
			}
			sum = sum + Math.max(z * (st - x), 0);
		//}

		//return Math.exp(-r * t) * (sum / nSimulations);
		return sum;
	}

	
	
	public ArrayList<Values> getValuesFromFile() throws IOException {
		
		ArrayList<Values> tempList = new ArrayList<Values>();
		String data = null;
		Values valuesTemp;
		
		final BufferedReader reader = new BufferedReader(new FileReader("monte_carlo_values.txt"));

		final String discardHeader = reader.readLine();
				
		while((data = reader.readLine()) != null){
						
			final String[] dataItems = data.split(",");
			
			valuesTemp = new Values(dataItems[0],
					Double.parseDouble(dataItems[1]), 
					Double.parseDouble(dataItems[2]), 
					Double.parseDouble(dataItems[3]), 
					Double.parseDouble(dataItems[4]), 
					Double.parseDouble(dataItems[5]), 
					Double.parseDouble(dataItems[6]), 
					Integer.parseInt(dataItems[7]), 
					Integer.parseInt(dataItems[8]));
			tempList.add(valuesTemp);
		}

		return tempList;
	}
		
	
	  
}
